<?php

    echo "<img src = \"linkhead.jpg\" alt = \"Links!\" /><br>";

function generateLinkList() {
    $linkArray = array();
    $linkArray = linkManager::getLinks($_SESSION['userID']);

    foreach ($linkArray as $link) {
        $address = $link->getAddress();

        $default = $default . "<a href='http://" . $address . "' target=\"_new\">" . $link->getName() . "</a><br>";
    }

    $default = $default . "<a href=index.php?add=link>Add Link</a> | <a href=index.php?edit=link>Edit Links</a>";

    return $default;
}

function generateEditDeleteList() {
    $linkArray = array();
    $linkArray = linkManager::getLinks($_SESSION['userID']);
    foreach ($linkArray as $link) {
        $default = $default . "<a href='http://" . $link->getAddress() . "'>"
             . $link->getName() . "</a> <a href=\"index.php?edit=link&id=" 
             . $link->getLinkID() . "\">Edit</a> <a href=\"index.php?delete=link&id=" 
             . $link->getLinkID() . "\">Delete</a><br>";
    }

    return $default;
}

function generateForm($action) {

    if (isset($_GET['id']))
    {
        $link = linkManager::getLink($_GET['id']);
        $name = " value=\"" . $link->getName() . "\"";
        $address = " value=\"" . $link->getAddress() . "\"";
    } else
    {
        $name = "";
        $address = "";
    }

    $form = "
        <form method=\"POST\" action=\"$action\"><table>
        <tr><td>Name:</td><td><input type=\"text\" name=\"name\" $name></td></tr>
        <tr><td>Address:</td><td><input type=\"text\" name=\"address\" $address></td></tr>
        </table>
        <input type=\"submit\" value=\"Submit\">
        </form>
        ";

    return $form;
}

if ($add == "link")
{
    if ($submit == "1")
    {
        if (isset($_POST['name']) && isset($_POST['address']))
        {
            $link = new Link();
            $link->setUserID($_SESSION['userID']);
            $link->setLinkID($_GET['id']);
            $link->setName($_POST['name']);
            $link->setAddress($_POST['address']);
            linkManager::addLink($link);
            echo generateLinkList();
        } else
        {
            echo "Please complete the entire form.<br>";
            $action = "index.php?add=link&submit=1";
            echo generateForm($action);
        }
    } else
    {
        $action = "index.php?add=link&submit=1";
        echo generateForm($action);
    }
} else if ($edit == "link")
{
    if (isset($_GET['id']))
    {
        if ($submit == "1")
        {
            if (isset($_POST['name']) && isset($_POST['address']))
            {
                $link = new Link();
                $link->setUserID($_SESSION['userID']);
                $link->setLinkID($_GET['id']);
                $link->setName($_POST['name']);
                $link->setAddress($_POST['address']);
                linkManager::editLink($link);
                echo generateLinkList();
            } else
            {
                echo "Please complete the entire form.<br>";
                $action = "index.php?edit=link&id=". $_GET['id'] . "&submit=1";
                echo generateForm($action);
            }
        } else
        {
            $action = "index.php?edit=link&id=". $_GET['id'] . "&submit=1";
            echo generateForm($action);
        }
    } else
    {        
        echo generateEditDeleteList();
    }

} else if ($delete == "link")
{
    if (isset($_GET['id']))
    {
        $link = linkManager::getLink($_GET['id']);
        $name = $link->getName();
        $address = $link->getAddress();
        if ($submit == "1")
        {
            linkManager::deleteLink($link);
            echo generateLinkList();
        } else
        {
            echo "Are you sure you want to delete this link?<br><br>$name<br>$address<br><br>
            <a href=index.php?delete=link&id=" . $_GET['id'] . "&submit=1>Yes</a> | <a href=index.php>No</a>";
        }
    } else
    {
        echo "You must select a link to delete.";
        echo generateEditDeleteList();
    }
} else
{
    echo generateLinkList();
}
?>
